<?php

namespace org\copperforest\oak\authorization;

use \org\copperforest\oak as ct;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class DAGAuthority extends Authority
{

    static protected $SELECT_CONTEXTS;
    static protected $CACHE_CONTEXTS = array( ); //SI UTILIZO ESTA CACHE AUNQUE CAMBIEN LOS ROLES DURANTE LA EJECUCION DEL SCRIPT....pues vaya

    function connect( DAGSecurityContext $context1, DAGSecurityContext $context2 )
    { //conectar otros contexto por debajo de estos
        //conecta los contextos basicos de este contexto con los contextos basicos del otro
        $from_contexts = $context1->split();
        $from_contexts_ids = array( );
        foreach ( $from_contexts as $b ) $from_contexts_ids[ ] = $b->getId();

        $to_contexts = $context2->split();
        $to_contexts_ids = array( );
        foreach ( $to_contexts as $b ) $to_contexts_ids[ ] = $b->getId();

        $result = ct\CT::$PDO->query( 'SELECT from_context_id, to_context_id, implicit FROM ct_security_contexts_edges WHERE from_context_id IN (' . implode( ',',
                        $from_contexts_ids ) . ') AND to_context_id IN (' . implode( ',',
                        $to_contexts_ids ) . ')' );
        $pares_conectados = $result->fetchAll( PDO::FETCH_NUM );
        $result->closeCursor();
        //ahora tenemos 


        $pairs = array( );
        $pairsSQL = array( );
        $numero = 0;

        foreach ( $from_contexts as $c1 )
                foreach ( $to_contexts as $c2 ) {

                //ahora el par c1, c2 no puede estar en la lista de pares conectados si hay una conexion directa entre ellos o en la direccion indicada o una conexion en la direccion opuesta
                $incluir = true;
                foreach ( $pares_conectados as $par )
                        if ( $c1 == $par[ 0 ] && $c2 == $par[ 1 ] && $par[ 2 ] == 'N' || $c1 == $par[ 1 ] && $c2 == $par[ 0 ] ) {
                        $incluir = false;
                        break;
                    }

                if ( $incluir ) {
                    if ( !isset( $pairs[ $c2 ] ) ) {
                        $pairs[ $c2 ] = array( $c1 );
                        $pairsSQL[ $c2 ] = array( '(' . $c1 . ',' . $c2 . '\'N\')' );
                    } else {
                        $pairs[ $c2 ][ ] = $c1;
                        $pairsSQL[ $c2 ][ ] = '(' . $c1 . ',' . $c2 . '\'N\')';
                    }

                    $numero++;
                }
            }


        //Ahora aqui tengo los pares que debo añadir
        //primero creo las aristas explicitas!

        $idUltimo = null;


        foreach ( $pairs as $c2 => $c1s ) {

            //primero creo las que llegan a los vertices de inicio
            $num = ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts_edges (from_context_id, to_context_id, implicit) VALUES ' . implode( ',',
                            $pairsSQL[ $c2 ] ) );
            //ahora num debería ser igual a $numero;

            $idPrimero = ct\CT::$PDO->lastInsertId();
            $idUltimo = $numero + $idPrimero - 1;

            //este num debería ser igual a todos
            $num = ct\CT::$PDO->exec( 'UPDATE ct_security_contexts_edges SET in_edge_id=edge_id, out_edge_id=edge_id WHERE edge_id BETWEEN ' . $idPrimero . ' AND ' . $idUltimo );

            //ahora tengo que crear las ariastas implicitas
            $currentId = $idPrimero;


            foreach ( $c1s as $c1 ) {
                $sql = 'INSERT INTO ct_security_contexts_edges ( in_edge_id, out_edge_id, from_context_id, to_context_id, implicit) ';
                $sql.= 'SELECT edge_id, ' . $currentId . ', from_context_id, ' . $c2 . ', \'Y\' FROM security_edges WHERE to_context_id=' . $c1;
                $num = ct\CT::$PDO->exec( $sql );

                $currentId++;

                if ( $num > 0 )
                        $idUltimo = ct\CT::$PDO->lastInsertId() + $num - 1;
            }

            $sql = 'INSERT INTO ct_security_contexts_edges ( in_edge_id, out_edge_id, from_context_id, to_context_id, implicit)';
            $sql.=' SELECT B.edge_id, A.edge_id, B.from_context_id, A.to_context_id, \'Y\' FROM zero_secutiry_contexts_edges AS A, zero_secutiry_contexts_edges AS B';
            $sql.=' WHERE B.edge_id BETWEEN ' . $idPrimero . ' AND ' . $idUltimo . ' AND A.from_context_id=' . $c2;

            $num = ct\CT::$PDO->exec( $sql );

            $idUltimo+=$num;
        }


        return $idUltimo;
    }

    function disconnect( DAGSecurityContext $context1,
            DAGSecurityContext $context2 )
    { //desconectar de este contexto otro
        //primero obtengo la interseccion de los contextos pasados por parametros con los contextos 
        $basic_contexts = $context1->split();

        $basic_ids = array( );

        foreach ( $basic_contexts as $b ) $basic_ids[ ] = $b->getId();

        $disconnect_contexts = $context2->split();

        $disconnect_ids = array( );

        foreach ( $disconnect_contexts as $b ) $disconnect_ids[ ] = $b->getId();



        $result = ct\CT::$PDO->query( 'SELECT from_context_id FROM ct_security_contexts_edges WHERE to_context_id IN (' . implode( ',',
                        $basic_ids ) . ')', PDO::FETCH_NUM );
        $suspect_contexts_ids = $result->fetchAll( PDO::FETCH_COLUMN, 0 );
        $suspect_contexts_ids = array_merge( $suspect_contexts_ids, $basic_ids );

        //obtengo los usuarios que verán afectadas sus caches por la desconexion.
        $result = ct\CT::$PDO->exec( 'SELECT user_id FROM ct_security_users_contexts WHERE context_id IN (' . implode( ',',
                        $suspect_contexts_ids ) . ') AND implicit=\'N\'' );
        $remove_users_cache = $result->fetchAll( PDO::FETCH_COLUMN, 0 );

        ct\CT::$PDO->exec( 'DELETE FROM ct_security_users_contexts WHERE user_id IN (' . implode( ',',
                        $remove_users_cache ) . ') AND implicit=\'Y\'' );



        //quiero eliminar las conexiones directas entre los contextos indicados y las implicitas
        $result = ct\CT::$PDO->query( 'SELECT  edge_id FROM ct_security_contexts_edges WHERE implicit=\'N\' AND from_context_id IN (' . implode( ',',
                        $basic_ids ) . ') AND to_context_id IN (' . implode( ',',
                        $disconnect_ids ) . ')', PDO::FETCH_NUM );

        if ( $result !== false ) {

            $remove_edges = $result->fetchAll( PDO::FETCH_COLUMN, 0 );

            if ( ZERO_CONF_AUX_TABLES ) {

                if ( ZERO_CONF_AUX_TEMP_TABLES ) {
                    ct\CT::$PDO->exec( 'CREATE TEMPORARY TABLE IF NOT EXISTS ct_security_contexts_deleted_edges_aux0 (id INT) ' );
                    ct\CT::$PDO->exec( 'CREATE TEMPORARY TABLE IF NOT EXISTS ct_security_contexts_deleted_edges_aux1 (id INT) ' );
                }

                $affected_rows = ct\CT::$PDO->exec( 'INSERT INTO `ct_security_contexts_deleted_edges_aux0` (id) VALUES (' . implode( '),(',
                                $remove_edges ) . ')' );

                while ( $affected_rows > 0 ) {
                    ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_deleted_edges_aux1 WHERE 1' );

                    ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts_deleted_edges_aux1 SELECT edge_id FROM ct_security_contexts_edges WHERE in_edge_id IN (SELECT id FROM ct_security_contexts_deleted_edges_aux0) OR  out_edge_id IN (SELECT id FROM ct_security_contexts_deleted_edges_aux0)' );

                    //eliminamos los que ya están en la tabla
                    ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_deleted_edges_aux1 WHERE edge_id IN ( SELECT id FROM ct_security_contexts_deleted_edges_aux0)' );

                    $affected_rows = ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts_deleted_edges_aux0 SELECT DISTINCT aux.id FROM ct_security_contexts_deleted_edges_aux1 as aux' );
                }

                ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_edges WHERE edge_id IN ( SELECT id FROM ct_security_contexts_deleted_edges_aux0)' );

                ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_deleted_edges_aux0 WHERE 1' );
            } else {

                $idstring = '';
                $adelante = true;


                while ( $adelante ) {
                    $adelante = false;
                    $idstring = implode( ',', $remove_edges );

                    $heredadas = ct\CT::$PDO->query( 'SELECT edge_id FROM ct_security_contexts_edges WHERE in_edge_id IN (' . $idstring . ') OR out_edge_id IN (' . $idstring . ')',
                            PDO::FETCH_NUM );

                    foreach ( $heredadas as $h ) {
                        if ( !in_array( $h[ 0 ], $remove_edges ) ) {
                            $remove_edges[ ] = $h[ 0 ];
                            $adelante = true;
                        }
                    }
                }

                ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_edges WHERE edge_id IN (' . $idstring . ');' );

                unset( $remove_edges );
            }
        }
    }

    function remove( DAGSecurityContext $context )
    {

        $remove_contexts = $context->split();

        $remove_ids = array( );
        foreach ( $remove_contexts as $r ) $remove_ids[ ] = $r->getId();


        $result = ct\CT::$PDO->query( 'SELECT from_context_id FROM ct_security_contexts_edges WHERE to_context_id IN (' . implode( ',',
                        $remove_ids ) . ')', PDO::FETCH_NUM );
        $suspect_contexts_ids = $result->fetchAll( PDO::FETCH_COLUMN, 0 );
        $suspect_contexts_ids = array_merge( $suspect_contexts_ids, $remove_ids );

        //obtengo los usuarios que verán afectadas sus caches por la desconexion.
        $result = ct\CT::$PDO->exec( 'SELECT user_id FROM ct_security_users_contexts WHERE context_id IN (' . implode( ',',
                        $suspect_contexts_ids ) . ') AND implicit=\'N\'',
                PDO::FETCH_NUM );
        $remove_users_cache = $result->fetchAll( PDO::FETCH_COLUMN, 0 );

        ct\CT::$PDO->exec( 'DELETE FROM ct_security_users_contexts WHERE user_id IN (' . implode( ',',
                        $remove_users_cache ) . ') AND implicit=\'Y\' OR context_id IN (' . implode( ',',
                        $remove_ids ) . ')' );


        //tengo que quitar las conexiones entre los contextos previos y estos y las conexiones entre estos y los siguientes        
        $result = ct\CT::$PDO->query( 'SELECT edge_id FROM ct_security_contexts_edges WHERE from_context_id IN (' . implode( ',',
                        $remove_ids ) . ') OR to_context_id IN (' . implode( ',',
                        $remove_ids ) . ')', PDO::FETCH_NUM );
        $remove_edges = $result->fetchAll( PDO::FETCH_COLUMN, 0 );

        if ( !empty( $remove_edges ) ) {


            if ( ZERO_CONF_AUX_TABLES ) {

                if ( ZERO_CONF_AUX_TEMP_TABLES ) {
                    ct\CT::$PDO->exec( 'CREATE TEMPORARY TABLE IF NOT EXISTS ct_security_contexts_deleted_edges_aux0 (id INT) ' );
                    ct\CT::$PDO->exec( 'CREATE TEMPORARY TABLE IF NOT EXISTS ct_security_contexts_deleted_edges_aux1 (id INT) ' );
                }

                $affected_rows = ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts_deleted_edges_aux0 (id) VALUES (' . implode( '),(',
                                $remove_edges ) . ')' );

                while ( $affected_rows > 0 ) {
                    ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_deleted_edges_aux1 WHERE 1' );

                    ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts_deleted_edges_aux1 SELECT edge_id FROM ct_security_contexts_edges WHERE in_edge_id IN (SELECT id FROM ct_security_contexts_deleted_edges_aux0) OR  out_edge_id IN (SELECT id FROM ct_security_contexts_deleted_edges_aux0)' );

                    //eliminamos los que ya están en la tabla
                    ct\CT::$PDO->exec( 'DELETE FROM `zero_secutiry_delete_edges_aux1` WHERE edge_id IN ( SELECT id FROM `zero_secutiry_delete_edges_aux0`)' );

                    $affected_rows = ct\CT::$PDO->exec( 'INSERT INTO ct_security_contexts_deleted_edges_aux0 SELECT DISTINCT aux.id FROM `zero_secutiry_delete_edges_aux1` as aux' );
                }

                ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_edges WHERE edge_id IN ( SELECT id FROM ct_security_contexts_deleted_edges_aux0)' );

                ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_deleted_edges_aux0 WHERE 1' );
            } else {

                $idstring = '';
                $adelante = true;


                while ( $adelante ) {
                    $adelante = false;
                    $idstring = implode( ',', $remove_edges );

                    $heredadas = ct\CT::$PDO->query( 'SELECT edge_id FROM ct_security_contexts_edges WHERE in_edge_id IN (' . $idstring . ') OR out_edge_id IN (' . $idstring . ')',
                            PDO::FETCH_NUM );

                    foreach ( $heredadas as $h ) {
                        if ( !in_array( $h[ 0 ], $aristas ) ) {
                            $remove_edges[ ] = $h[ 0 ];
                            $adelante = true;
                        }
                    }
                }

                unset( $remove_edges );


                ct\CT::$PDO->exec( 'DELETE FROM ct_security_contexts_edges WHERE edge_id IN (' . $idstring . ');' );
            }
        }
    }

    function query_one( $context, $id )
    {
        //a través del identificador de la politica tengo que obtener la configuracion 

        $conf = unserialize( file_get_contents( CF_CONFIG_PATH . $this->id . '.authority' ) );

        //primero para cada clase obtengo el contexto que tengo que utilizar y con que contexto lo obtengo
        $basic_contexts = $context->split();
        $basic_ids = array( );
        foreach ( $basic_contexts as $b ) $basic_ids[ ] = $b->getId();

        $result = ct\CT::$PDO->query( 'SELECT to_context_id FROM ct_security_contexts_edges WHERE from_context_id IN (' . implode( ',',
                        $basic_ids ) . ')', PDO::FETCH_NUM );
        $basic_ids = array_merge( $basic_ids,
                $result->fetchAll( PDO::FETCH_COLUMN, 0 ) );

        $result = ct\CT::$PDO->query( 'SELECT classname, resource_tag, resource_id FROM ct_security_privileges WHERE context_id IN (' . implode( ',',
                        $basic_ids ) . ') AND classname IN (' . $conf[ 'classes_sql' ] . ')',
                PDO::FETCH_NUM );
        unset( $basic_contexts, $basic_ids );

        //ahora, el orden de las clases es importante
        $basic_resources = array( );
        $resourcesSQL = array( );

        foreach ( $result as $r ) {

            if ( !isset( $basic_resources[ $r[ 0 ] ] ) ) {
                $basic_resources[ $r[ 0 ] ] = array( );
                $resourcesSQL[ $r[ 0 ] ] = 'to_resource_tag=' . ct\CT::$PDO->quote( $r[ 1 ] ) . ' AND to_resource_id=' . $r[ 2 ];
            }

            if ( !isset( $basic_resources[ $r[ 0 ] ][ $r[ 1 ] ] ) )
                    $basic_resources[ $r[ 0 ] ][ $r[ 1 ] ] = array( );

            $basic_resources[ $r[ 0 ] ][ $r[ 1 ] ][ ] = $r[ 2 ];
        }

        $resources = array( );

        foreach ( $basic_resources as $class => $array ) {
            $resources[ $class ] = $array;


            $result = ct\CT::$PDO->query( 'SELECT from_resource_id, from_resource_tag FROM ct_security_resources_edges WHERE ' . implode( ' OR ',
                            $resourcesSQL[ $class ] ), PDO::FETCH_NUM );
            foreach ( $result as $r ) {

                if ( !isset( $resources[ $class ][ $r[ 0 ] ] ) )
                        $resources[ $class ][ $r[ 0 ] ] = array( );

                $resources[ $class ][ $r[ 0 ] ][ ] = $r[ 1 ];
            }
        }

        unset( $basic_resources, $resourcesSQL );

        $used_resources = array( );
        $sql_ids = array( );

        foreach ( $conf[ 'classes' ] as $class => $where ) { //en funcion de la ordenación de las clases obtenemos los recursos para cada una de ellas
            if ( !empty( $resources[ $class ] ) ) {
                //$resources[$class] tiene todos los recursos alcanzables con este contexto y clase.

                $coma = false;
                foreach ( $resources[ $class ] as $tag => $ids ) {

                    $coma = false;
                    $sqlids = '';

                    foreach ( $ids as $id ) {

                        if ( !isset( $used_resources[ $tag ] ) || !in_array( $id,
                                        $used_resources[ $tag ] ) ) {

                            if ( $coma ) $sqlids = ',' . $id;
                            else {
                                $coma = true;
                                $sqlids = $id;
                            }
                        }
                    }

                    if ( $coma ) {

                        $sqlids = ' IN (' . $sqlids . ')';

                        if ( !empty( $where ) )
                                $sqlids.=' AND (' . $where . ')';

                        else { //si no hay where los recursos seleccionados por esta clase no serán accesibles con las otras clases
                            foreach ( $ids as $id ) {
                                if ( !isset( $used_resources[ $tag ] ) )
                                        $used_resources[ $tag ] = array( );

                                if ( !in_array( $id, $used_resources[ $tag ] ) )
                                        $used_resources[ $tag ][ ] = $id;
                            }
                        }


                        $sql_ids[ $class ][ $tag ] = $sqlids;
                    }
                }
            }
        }

        //ahora tengo las sql de los ids implicados con el where extra
        $num_fields = count( $conf[ 'resource_fields' ] );
        $num_classes = count( $sql_ids );
        $n = 1;

        $select = '';
        $where = '';
        $orwhere = false;


        if ( $num_fields == 1 ) {


            foreach ( $sql_ids as $class => $resources ) {

                $or = false;
                $selectclass = '';

                foreach ( $resources as $tag => $sql ) {

                    if ( $or )
                            $selectclass .= ' OR ' . $conf[ 'resource_fields' ][ 0 ][ 0 ] . '=0x' . bin2hex( $tag ) . ' AND ' . $conf[ 'resource_fields' ][ 0 ][ 1 ] . $sql;
                    else {
                        $or = true;
                        $selectclass = $conf[ 'resource_fields' ][ 0 ][ 0 ] . '=0x' . bin2hex( $tag ) . ' AND ' . $conf[ 'resource_fields' ][ 0 ][ 1 ] . $sql;
                    }
                }

                if ( $n == $num_classes ) {
                    $select = $select . '\'' . $class . '\'';
                } else {
                    $select = $select . 'IF(' . $selectclass . ',\'' . $class . '\','; //ojo en la ultima no tengo que añadir el selectclass
                    $n++;
                }

                if ( $orwhere ) $where.=' OR ' . $selectclass;
                else {
                    $where = $selectclass;
                    $orwhere = true;
                }
            }

            for ( $i = 0; $i < $num_classes - 1; $i++ ) $select.=')';
        } else {


            foreach ( $sql_ids as $class => $resources ) {


                $and = false;
                $selectclass = '';

                for ( $i = 0; $i < $num_fields; ++$i ) {

                    if ( $and ) {
                        $selectclass.=') AND (';
                    } else {
                        $selectclass = '(';
                        $and = true;
                    }

                    $or = false;

                    foreach ( $resources as $tag => $sql ) {

                        if ( $or )
                                $selectclass.=' OR ' . $conf[ 'resource_fields' ][ $i ][ 0 ] . '=0x' . bin2hex( $tag ) . ' AND ' . $conf[ 'resource_fields' ][ $i ][ 1 ] . $sql;
                        else {
                            $or = true;
                            $selectclass.=$conf[ 'resource_fields' ][ $i ][ 0 ] . '=0x' . bin2hex( $tag ) . ' AND ' . $conf[ 'resource_fields' ][ $i ][ 1 ] . $sql;
                        }
                    }

                    $selectclass.=')';
                }

                if ( $n == $num_classes ) {
                    $select = $select . '\'' . $class . '\'';
                } else {
                    $select = $select . 'IF(' . $selectclass . ',\'' . $class . '\','; //ojo en la ultima no tengo que añadir el selectclass
                    $n++;
                }

                if ( $orwhere ) $where.=' OR ' . $selectclass;
                else {
                    $where = $selectclass;
                    $orwhere = true;
                }
            }
        }

        if ( !empty( $conf[ 'where' ] ) )
                $where = '(' . $where . ') AND (' . $conf[ 'where' ] . ')';

        $sql = $conf[ 'base' ] . ' ' . $select . ' FROM ' . $conf[ 'from' ] . ' WHERE ' . $where;


        $r = array( $sql, $conf[ 'idField' ], $conf[ 'end' ] );





        list($sql, $idField, $end) = SecurityCache::getResourceInfo( $this->id,
                        $nodeId );


        $k = 0;
        foreach ( $idField as $field => $isNumeric ) {
            if ( $isNumeric )
                    $sql.=' AND ' . $field . '=' . ( ( int ) $id[ $k ] );
            else if ( !empty( $id[ $k ] ) )
                    $sql.=' AND ' . $field . '=0x' . bin2hex( $id[ $k ] );

            $k++;
        }


        if ( !empty( $end ) ) $sql.=' ' . $end;

        $sql = 'SELECT SQL_SMALL_RESULT ' . $sql . ' LIMIT 1';

        if ( self::$_METHOD == Node::POST && !Node::isPrepared() ) //solo lo que sea antes de la ejecución del método __prepare
                $sql.=' FOR UPDATE';

        $sleeptime = 1;
        while ( true ) {
            try {
                $result = ct\CT::$PDO->query( $sql );
                break;
            } catch ( DeadLockException $e ) {

                $sleeptime = $sleeptime * 5;
                if ( $sleeptime > 125 ) throw $e;
            }
        }

        $node = $result->fetch( PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE );

        //no hay que establecerle la politica, id, etc
        return $node;
    }

    function getCollection( $context, &$datos, $params = array( ) )
    {


        $sleeptime = 1;
        while ( true ) {
            try {
                $result = ct\CT::$PDO->query( $sql );
                break;
            } catch ( DeadLockException $e ) {

                $sleeptime = $sleeptime * 5;
                if ( $sleeptime > 125 ) throw $e;
            }
        }
    }

    //tengo 
    static function configure( $file )
    {
        
    }

    static function deploy( $engine )
    {


        ct\CT::$PDO->exec(
                'CREATE TABLE `ct_resources_edges` (
                `edge_id` int(11) NOT NULL AUTO_INCREMENT,
                `in_edge_id` int(11) DEFAULT NULL,
                `out_edge_id` int(11) DEFAULT NULL,
                `from_resource_id` int(50) NOT NULL,
                `from_resource_tag` varchar(50) NOT NULL,
                `to_resource_id` int(50) NOT NULL,
                `to_resource_tag` varchar(50) NOT NULL,
                `implicit` enum(\'Y\',\'N\') CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
                PRIMARY KEY (`edge_id`)
            ) ENGINE=' . $engine . ' DEFAULT CHARSET=ascii' );

        ct\CT::$PDO->exec(
                'CREATE TABLE `ct_resources_cache` (
                `context_id` int(11) NOT NULL,
                `authority_id` int(11) NOT NULL,
                `id_fields` varbinary(255),
                `statement` varbinary(1000),
                `statement_end` varbinary(255),
                PRIMARY KEY (`context_id`,`policy_id`)
            ) ENGINE=' . $engine . ' DEFAULT CHARSET=ascii' );

        //                `id_numeric` varbinary(127),

        ct\CT::$PDO->exec(
                'CREATE TABLE `ct_privileges` (
                `rol_id` int(50) NOT NULL,
                `resource_id` int(50) NOT NULL,
                `classname` varbinary(255),
                PRIMARY KEY (`context_id`,`resource_id`,`classname`)
            ) ENGINE=' . $engine . ' DEFAULT CHARSET=ascii' );


        if ( ZERO_CONF_AUX_TABLES && !ZERO_CONF_AUX_TEMP_TABLES ) {

            ct\CT::$PDO->exec(
                    'CREATE TABLE `ct_delete_edges_aux0` (
                `id` int(11) NOT NULL,
                PRIMARY KEY (`id`)
                ) ENGINE=' . $engine . ' DEFAULT CHARSET=ascii' );

            ct\CT::$PDO->exec(
                    'CREATE TABLE `ct_delete_edges_aux1` (
                `id` int(11) NOT NULL,
                PRIMARY KEY (`id`)
                ) ENGINE=' . $engine . ' DEFAULT CHARSET=ascii' );
        }

        Resource::_deploy();
    }

    static function prepareStatements()
    {
        
    }

}

?>
